package kd.hc.ecco;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.exception.KDBizException;
import kd.bos.id.ID;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.util.StringUtils;
import kd.hc.utils.OperationServiceUtil;
import kd.sdk.plugin.Plugin;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 目标成本变更 服务插件 校验
 */
public class TargetCostUpdateBillServiceValidatorPlugIn extends AbstractOperationServicePlugIn implements Plugin {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().add("hc95_rank_en");
        e.getFieldKeys().add("hc95_longid_en");
        e.getFieldKeys().add("pid");
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new TargetCostUpdateBillServiceValidator());
    }

    private class TargetCostUpdateBillServiceValidator extends AbstractValidator {

        public void checkBill() {
            for (ExtendedDataEntity dataEntity : this.getDataEntities()) {
                DynamicObject targetCost = dataEntity.getDataEntity();
                String id = targetCost.getString("id");
                DynamicObjectCollection entryentity = targetCost.getDynamicObjectCollection("hc95_tree_entryentity");//明细信息
                if (entryentity != null && entryentity.size() >= 1) {
                    List<String> rooIds = entryentity.stream().filter(f1 -> "1".equals(f1.getString("hc95_rank_en"))).map(m1 -> m1.getString("id")).collect(Collectors.toList());
                    for (String rooId : rooIds) {
                        String costItemName = entryentity.stream().filter(f1 -> rooId.equals(f1.getString("id"))).map(m1 -> m1.getDynamicObject("hc95_cost_item_en").getString("name")).collect(Collectors.toList()).stream().findFirst().get();
                        List<String> twoLevel = entryentity.stream().filter(f1 -> f1.getString("pid").equals(rooId)).map(m1 -> m1.getString("id")).collect(Collectors.toList());
                        if (twoLevel != null && twoLevel.size() >= 1) {
                            for (String twoLevelId : twoLevel) {
                                List<String> threeLevel = entryentity.stream().filter(f1 -> f1.getString("pid").equals(twoLevelId)).map(m1 -> m1.getString("id")).collect(Collectors.toList());
                                if (threeLevel == null || threeLevel.size() <= 0) {
                                    this.addErrorMessage(dataEntity, "成本类型[" + costItemName + "]树形有且只能有三级!");
                                }
                            }
                        } else {
                            this.addErrorMessage(dataEntity, "成本类型[" + costItemName + "]树形有且只能有三级!");
                        }
                    }
                }
            }

        }

        @Override
        public void validate() {
            String operateKey = this.getOperateKey();
            switch (operateKey) {
                case "save":
                    checkBill();
                    break;
                case "submit":
                    checkBill();
                    break;
            }
        }
    }
}